package com.tang.companyjob.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.tang.common.entity.companyJob.Company;
import com.tang.common.entity.companyJob.Job;
import com.tang.companyjob.mapper.CompanyMapper;
import com.tang.companyjob.mapper.JobMapper;

import com.tang.companyjob.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobService {
    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public List<Job> list(String keywords, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        if (keywords == null || "".equals(keywords)) {
            QueryWrapper<Job> qw = new QueryWrapper<Job>();
            qw.eq("delete_flag", 1);
            qw.orderByDesc("create_time");
            List<Job> jobs = jobMapper.selectList(qw);
            setCompany(jobs);
            return jobs;
        }
        QueryWrapper<Job> queryWrapper = new QueryWrapper<Job>();
        queryWrapper.eq("delete_flag", 1).orderByDesc("create_time").
                and(qw -> qw.like("job_name", keywords).or().like("user_name", keywords).or().like("company_name", keywords));
        List<Job> jobs = jobMapper.selectList(queryWrapper);
        setCompany(jobs);
        return jobs;
    }

    @Override
    public List<Job> listBegin(String keywords, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        if (keywords == null || "".equals(keywords)) {
            QueryWrapper<Job> qw = new QueryWrapper<Job>();
            qw.eq("delete_flag", 1);
            qw.eq("job_state",1);
            qw.orderByDesc("create_time");
            List<Job> jobs = jobMapper.selectList(qw);
            setCompany(jobs);
            return jobs;
        }
        QueryWrapper<Job> queryWrapper = new QueryWrapper<Job>();
        queryWrapper.eq("delete_flag", 1).eq("job_state",1).orderByDesc("create_time").
                and(qw -> qw.like("job_name", keywords).or().like("user_name", keywords).or().like("company_name", keywords));
        List<Job> jobs = jobMapper.selectList(queryWrapper);
        setCompany(jobs);
        return jobs;
    }

    @Override
    public List<Job> listByCompanyName(String keywords, Integer currentPage, Integer pageSize, String companyName) {
        PageHelper.startPage(currentPage, pageSize);
        if (keywords == null || "".equals(keywords)) {
            QueryWrapper<Job> qw = new QueryWrapper<Job>();
            qw.eq("delete_flag", 1);
            qw.eq("company_name", companyName);
            qw.orderByDesc("create_time");
            return jobMapper.selectList(qw);
        }
        QueryWrapper<Job> queryWrapper = new QueryWrapper<Job>();
        queryWrapper.eq("delete_flag", 1).eq("company_name", companyName).orderByDesc("create_time").
                and(qw -> qw.like("job_name", keywords));
        List<Job> jobs = jobMapper.selectList(queryWrapper);
        return jobs;
    }

    @Override
    public int deleteByJobId(int jobId) {
        Job job = jobMapper.selectById(jobId);
        job.setDeleteFlag(0);
        return jobMapper.updateById(job);
    }

    @Override
    public int shelveByJobId(int jobId) {
        Job job = jobMapper.selectById(jobId);
        int result = 0;
        if (job != null && job.getDeleteFlag() != 0) {
            job.setJobState(job.getJobState() == 1 ? 0 : 1);
            job.setUpdateTime(new Date());
            result = jobMapper.updateById(job);
        }
        return result;
    }

    @Override
    public Integer getJobCount() {
        return jobMapper.selectCount(null);
    }

    @Override
    public Job getJob(Integer jobId) {
        QueryWrapper<Job> q = new QueryWrapper<>();
        q.eq("delete_flag", 1);
        q.eq("job_state",1);
        q.eq("job_id",jobId);
        Job job = jobMapper.selectOne(q);
        if(job!=null){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("company_name", job.getCompanyName());
            Company company = companyMapper.selectOne(queryWrapper);
            job.setCompany(company);
        }
        return job;
    }

    public void setCompany(List<Job> jobs) {
        for (Job job : jobs) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("company_name", job.getCompanyName());
            Company company = companyMapper.selectOne(queryWrapper);
            job.setCompany(company);
        }
    }

}
